<?php

namespace app\index\controller;

use app\index\Service\Index as IndexService;
use think\Controller;
use think\Db;
use think\db\Where;
use think\facade\Validate;

class Index extends Controller
{
    public function index()
    {

        if ($this->request->isPost()) {
            $data = input();
            $rule = [
                'ksh' => 'require|max:10|token',
//                'ksh' => 'require|max:10',
                'xm' => 'require|max:10',
            ];
            $msg = [
                'ksh.require' => '考生号不能为空',
                'ksh.max' => '考生号长度过长',
                'xm.require' => '姓名不能为空',
                'xm.max' => '姓名长度过长',
            ];

            $validate = Validate::make($rule, $msg);
            $result = $validate->check($data);
            if ($result !== true) {
                $this->error($validate->getError());
            }

            if (!$info = IndexService::getResult($data['ksh'], $data['xm'])) {
                $this->error('查询失败');
            }

            // 专业信息
            $data_list = Db::name('hlcb_result_detail')
                ->where('ksh', $info['ksh'])
                ->order('kmdm asc')
                ->select();

            $this->assign('info', $info);
            $this->assign('data_list', $data_list);

            // 专业全部人数
            $headcount = Db::name('hlcb_result')
                ->where('zydm', $info['zydm'])
                ->where('status', 1)
                ->count();
            $this->assign('headcount', $headcount);

            // 过线人数

            $where = null;
            // 过线条件
            foreach ($data_list as $item) {
                if ($item['kmdm'] >= 10 && $item['kmdm'] <= 99) {
                    $where = self::getGxWhere($item['kmdm']);
                }
            }

            if (!empty($where)) {
                $headcount_gx = Db::name('hlcb_result')
                    ->where($where)
                    ->where('zydm', $info['zydm'])
                    ->where('status', 1)
                    ->count();
            }else{
                $headcount_gx = '未知';
            }

            $this->assign('headcount_gx', $headcount_gx);


            // 五科排名
            $rank = Db::query("SELECT u.*
FROM(
SELECT ksh,wkzf,(@ranknum:= @ranknum+ 1) AS rank
FROM n_hlcb_result,(
SELECT(@ranknum := 0)) b
where zydm = {$info['zydm']}
ORDER BY wkzf desc,skzf DESC) u
WHERE u.ksh= {$info['ksh']}
");
            $this->assign('rank', $rank[0]['rank']);

            // 省考排名
            $sk_rank = Db::query("SELECT u.*
FROM(
SELECT ksh,wkzf,(@ranknum:= @ranknum+ 1) AS rank
FROM n_hlcb_result,(
SELECT(@ranknum := 0)) b
where zydm = {$info['zydm']}
ORDER BY skzf DESC,ksh desc) u
WHERE u.ksh= {$info['ksh']}
");
            $this->assign('sk_rank', $sk_rank[0]['rank']);

            // 录取查询
            $tdx_list = [
                '13656001' => 201,
                '13656002' => 230,
                '13656003' => 310,
                '13656004' => 208,
                '13656005' => 281,
                '13656006' => 247,
                '13656007' => 307,
                '13656008' => 268,
                '13656009' => 256,
                '13656010' => 212,
                '13656011' => 317,
                '13656012' => 209,
                '13656013' => 206,
                '13656014' => 293,
                '13656015' => 220,
                '13656016' => 209,
                '13656017' => 288,
            ];

            $isLq = false;



            if (!empty($tdx_list[$info['zydm']]) && $info['wkzf'] >= $tdx_list[$info['zydm']]) {
                $isLq = true;
            }

            $this->assign('isLq', $isLq);

            return $this->fetch('index/result');

        } else {

            // =========== GET =================
            $headcount = Db::name('hlcb_result')
                ->where('status', 1)
                ->count();
            $this->assign('headcount', $headcount);
            return $this->fetch();
        }

    }

    // 获取过线条件
    private static function getGxWhere($kmdm)
    {
        $where = new Where();
        switch ($kmdm) {
            case 11:
            case 13:
            case 15:
                $where['wkzf'] = ['>=', 200];
                $where['skzf'] = ['>=', 100];
                break;
            case 12:
                $where['wkzf'] = ['>=', 180];
                $where['skzf'] = ['>=', 90];
                break;
        }

        return $where;
    }

}
